#include "liangzai.h"

#include <qdatetime.h>
#include <QCryptographicHash>
#include <qdebug.h>

LiangZai::LiangZai(QWidget *parent)
{
    QString appKey = "349138dcc831ac39c71a2072";
    auto masterSecret =  "bf0a4b7a1db0a7c4841d5121";

    Client  client;
    //client->login()
    auto time = QDateTime::currentDateTime().toMSecsSinceEpoch();
    auto str = QString("appkey=%1&timestamp=%2&random_str=022cd9fd995849b58b3ef0e943421ed9&key=%3")
            .arg(appKey).arg(time).arg(masterSecret);

    Jmcpp::Authorization auth;
    auth.appKey = appKey.toStdString();
    auth.randomStr = "022cd9fd995849b58b3ef0e943421ed9";
    auth.timestamp = QString::number(time).toStdString();
    auth.signature = QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Md5).toHex().toStdString();
    client.onLogined([=](bool firstLogin){
        qDebug()<<"wlf"<<firstLogin;
    });
    client.onMessageReceive([=](MessagePtr firstLogin){});
    try{
            client.login("0001wlf", "123456", auth,true).get();
            auto content = client.createTextContent("hello underground world!").get();
            auto msg = client.buildMessage(Jmcpp::UserId("test001"), content);
            client.sendMessage(msg).get();
            client.logout();
        }
        catch(Jmcpp::ServerException& e)
        {
            // API 服务器调用返回的错误
            std::cout << e.what() << std::endl;
        }
        catch(std::system_error& e)
        {
            // API 调用产生的其它错误
            std::cout << e.what() << std::endl;
        }
//    qWarning()<<"wlf"<<client.isLogined();
//    client.login("0001wlf","123456", auth, true);
    //    client.
}

